$.fn.cropperImg = function(options) {
	// 默认参数
	var defaults = {
		url: "", //提交截图等数据的url
		data: {}, //参数
		ajaxSuccess: function() {}, // 上传函数
		ajaxError:function(){alert("ajax error")},
		inputImage: $('#inputImage'), // 上传按钮的id
		initOptions: {
			aspectRatio: 16 / 9,
			preview: '.img-preview'
		}
	}
	var $image = $(this);

	options = $.extend(defaults, options);

	$image.on({
		'build.cropper': function(e) {
			console.log(e.type);
		},
		'built.cropper': function(e) {
			console.log(e.type);
		},
		'dragstart.cropper': function(e) {
			console.log(e.type, e.dragType);
		},
		'dragmove.cropper': function(e) {
			console.log(e.type, e.dragType);
		},
		'dragend.cropper': function(e) {
			console.log(e.type, e.dragType);
		},
		'zoomin.cropper': function(e) {
			console.log(e.type);
		},
		'zoomout.cropper': function(e) {
			console.log(e.type);
		}
	}).cropper(options.initOptions);

	// Import image
	var $inputImage = options.inputImage,
		URL = window.URL || window.webkitURL,
		blobURL;

	if(URL) {
		$inputImage.change(function() {
			var files = this.files,
				file;

			if(files && files.length) {
				file = files[0];

				if(/^image\/\w+$/.test(file.type)) {
					blobURL = URL.createObjectURL(file);
					$image.one('built.cropper', function() {
						URL.revokeObjectURL(blobURL); // Revoke when load complete
					}).cropper('reset', true).cropper('replace', blobURL);
					$inputImage.val('');
				} else {
					alert('Please choose an image file.');
				}
			}
		});
	} else {
		$inputImage.parent().remove();
	}

	// Methods
	$(document.body).on('click', '[data-method]', function() {
		var data = $(this).data(),
			$target,
			result;

		if(data.method) {
			data = $.extend({}, data); // Clone a new one

			if(typeof data.target !== 'undefined') {
				$target = $(data.target);

				if(typeof data.option === 'undefined') {
					try {
						data.option = JSON.parse($target.val());
					} catch(e) {
						console.log(e.message);
					}
				}
			}

			result = $image.cropper(data.method, data.option);
			if(data.method === 'getCroppedCanvas') {
				if(typeof(options.ajaxSuccess) == 'function') {
					result.toBlob(function(blob) {
						var formData = new FormData(); // 参数
						formData.append('uploadFile', blob);
						//设置数据
						var param = options.data;
						for(var key in param){  
							formData.append(key,param[key]);
						}  
						$.ajax({
							url:options.url,
							method: "POST",
							data: formData,
							dataType: "json",
							processData: false,
							contentType: false,
							async: false,
							success: function(res) {
								//处理返回结果
								options.ajaxSuccess(res);
							},
							error: function() {
								options.ajaxError();
							}
						});

					});
				}
			}

			if($.isPlainObject(result) && $target) {
				try {
					$target.val(JSON.stringify(result));
				} catch(e) {
					alert(e.message);
				}
			}

		}
	});
}